Ext.define('DutyList.calendar.template.BoxLayout', {
        extend:'Ext.XTemplate',

        requires:['Ext.Date'],

        constructor:function (config) {

            Ext.apply(this, config);
            var weekLinkTpl = this.showWeekLinks ? '<div id="{weekLinkId}" class="ext-cal-week-link">{weekNum}</div>' : '';
            this.callParent([
                '<tpl for="weeks">',
                    '<div id="{[this.id]}-wk-{[xindex-1]}" class="ext-cal-wk-ct" style="top:{[this.getRowTop(xindex, xcount)]}; height:{[this.getRowHeight(xcount)]};">',
                        weekLinkTpl,
                        '<table class="ext-cal-bg-tbl" cellpadding="0" cellspacing="0">',
                            '<tbody>',
                                '<tr>',
                                    '<tpl for=".">',
                                        '<td id="{[this.id]}-day-{startDate:date("Ymd")}" class="{cellCls}" >',
                                            '<div id="{[this.id]}-day-{startDate:date("Ymd")}-daytime" class="{daytimeCls}" >',
                                                '<div class="{titleCls}">{title}</div>',
                                            '</div>',
                                            '<div id="{[this.id]}-day-{startDate:date("Ymd")}-nighttime" class="{nighttimeCls}"></div>',
                                        '</td>',
                                    '</tpl>',
                                '</tr>',
                            '</tbody>',
                        '</table>',
                    '</div>',
                '</tpl>', {
                    getRowTop:function (i, ln) {
                        return (this.maxCountShedulesPerDuty === undefined || this.fullSize) ? ((i - 1) * (100 / ln)) + '%' : (i - 1) * this.maxCountShedulesPerDuty*2 * 20+'px';
                    },
                    getRowHeight:function (ln) {
                        return (this.maxCountShedulesPerDuty === undefined || this.fullSize) ? (100 / ln + '%') : this.maxCountShedulesPerDuty*2 *20+'px';
                    }
                }
            ]);
        },

        applyTemplate:function (o) {
            Ext.apply(this, o);
            var w = 0, title = '', first = true, isToday = false, showMonth = false, prevMonth = false, nextMonth = false,
                weeks = [
                    []
                ], holiday = false,
                dt = Ext.Date.clone(this.viewStart),
                thisMonth = this.Date.getMonth();

            for (; w < this.weekCount || this.weekCount == -1; w++) {
                if (dt > this.viewEnd) {
                    break;
                }
                weeks[w] = [];

                for (var d = 0; d < this.dayCount; d++) {
                    isToday = dt.getTime() === DutyList.calendar.util.Date.today().getTime();
                    showMonth = first || (dt.getDate() == 1);
                    prevMonth = (dt.getMonth() < thisMonth) && this.weekCount == -1;
                    nextMonth = (dt.getMonth() > thisMonth) && this.weekCount == -1;

                    if (dt.getDay() == 1) {
                        // The ISO week format 'W' is relative to a Monday week start. If we
                        // make this check on Sunday the week number will be off.
                        weeks[w].weekNum = this.showWeekNumbers ? Ext.Date.format(dt, 'W') : '&#160;';
                        weeks[w].weekLinkId = 'ext-cal-week-' + Ext.Date.format(dt, 'Ymd');
                    }

                    if (dt.getDay() == 0 || dt.getDay() == 6) {
                        holiday = true;
                    }

                    if (showMonth) {
                        if (isToday) {
                            title = this.getTodayText();
                        }
                        else {
                            title = Ext.Date.format(dt, this.dayCount == 1 ? 'l, F j, Y' : (first ? 'M j, Y' : 'M j'));
                        }
                    }
                    else {
                        var dayFmt = (w == 0 && this.showHeader !== true) ? 'D j' : 'j';
                        title = isToday ? this.getTodayText() : Ext.Date.format(dt, dayFmt);
                    }
                    weeks[w].push({
                        title:title,
                        startDate:Ext.Date.clone(dt),
                        titleCls:'ext-cal-dtitle ' + (isToday ? ' ext-cal-dtitle-today' : '') +
                            (w == 0 ? ' ext-cal-dtitle-first' : '') +
                            (prevMonth ? ' ext-cal-dtitle-prev' : '') +
                            (nextMonth ? ' ext-cal-dtitle-next' : ''),
                        cellCls:'ext-cal-day' + (isToday ? '-today' : ''),
                        daytimeCls:'ext-cal-day-daytime' +' ext-cal-day-daytime' + (holiday ? '-holiday' : '') +
                            (prevMonth ? '-prev' : '') +
                            (nextMonth ? '-next' : ''),
                        nighttimeCls:'ext-cal-day-nighttime' + ' ext-cal-day-nighttime' + (holiday ? '-holiday' : '') +
                            (prevMonth ? '-prev' : '') +
                            (nextMonth ? '-next' : '')
                    });
                    dt = DutyList.calendar.util.Date.add(dt, {days:1});
                    first = false;
                    holiday = false;
                }
            }
            return this.applyOut({
                weeks:weeks
            }, []).join('');
        },

        getTodayText:function () {
            var dt = Ext.Date.format(new Date(), 'l, F j, Y'),
                todayText = this.showTodayText !== false ? this.todayText : '',
                timeText = this.showTime !== false ? ' <span id="' + this.id + '-clock" class="ext-cal-dtitle-time">' +
                    Ext.Date.format(new Date(), 'g:i a') + '</span>' : '',
                separator = todayText.length > 0 || timeText.length > 0 ? ' &mdash; ' : '';

            if (this.dayCount == 1) {
                return dt + separator + todayText + timeText;
            }
            fmt = this.weekCount == 1 ? 'D j' : 'j';
            return todayText.length > 0 ? todayText + timeText : Ext.Date.format(new Date(), fmt) + timeText;
        }
    },
    function () {
        this.createAlias('apply', 'applyTemplate');
    });